package com.lsfb.light.Utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.lsfb.light.R;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by nonsense on 2017/8/25.
 */

public class ProgressDialogManager {

    private ProgressDialog dialog;
    public static List<ProgressDialog> dialogs;
    private Activity activity;


//    public ProgressDialogManager(Context context) {
//        super(context);
//        init(context);
//    }


    public ProgressDialogManager(Context context) {
        init(context);
    }

    private void init(Context context) {
        if (context instanceof Activity) {
            this.activity = (Activity) context;
        }
        dialog = new ProgressDialog(context);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setIcon(R.mipmap.icon_app);
        dialog.setTitle("提示");
        dialog.setMessage("加载中...");
        if (dialogs == null) {
            dialogs = new ArrayList<>();
        }
        dialogs.add(dialog);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                LsfbLog.e("activity" + activity.getClass().getName());
                dialogs.remove(dialog);
            }
        });
    }

    public void setTips(String title, String msg) {
        if (dialog != null) {
            dialog.setTitle(title);
            dialog.setMessage(msg);
        }
    }


    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void hideDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
